<?php

namespace App\Console\Commands;

use GuzzleHttp\Client;
use Illuminate\Console\Command;

class ESInit extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'es:init';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'init laravel es for post.';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $host = config('scout.elasticsearch.hosts')[0];

        // 1 [http]创建es_template
        $client = new Client();
        // 先删除
        $url = $host . '/_template/tmp';
        try {
            $client->delete($url);
        } catch (\Throwable $e) {
            $this->error('delete template return:' . $e->getMessage());
        }
        // 再新建
        $options = [
            'json' => [
                'template' => config('scout.elasticsearch.index'),
                "_default_" => [
                    "dynamic_templates" => $this->getDynamicTemplates(),
                ],
            ]
        ];
        $response = $client->put($url, $options);
        $this->info('put template message: ' . $response->getBody()->getContents());
        $this->info('create template success.');


        // 2 [http]创建es_index
        // 先删除
        $url = $host . '/' . config('scout.elasticsearch.index');
        try {
            $client->delete($url);
        } catch (\Throwable $e) {
            $this->error('delete index return:' . $e->getMessage());
        }
        // 再新建
        $options = [
            'json' => $this->getIndexSettings(),
        ];
        $response = $client->put($url, $options);
        $this->info('put index message: ' . $response->getBody()->getContents());
        $this->info('create index success.');
    }

    protected function getDynamicTemplates()
    {
        return [
            [
                "strings" => [
                    "match_mapping_type" => "string",
                    "mapping" => [
                        "type" => "text",
                        "analyzer" =>"ik_smart",
                        "fields" => [
                            "keyword" => [
                                "type" => "keyword"
                            ]
                        ]
                    ]
                ]
            ]
        ];
    }

    public function getIndexSettings()
    {
        return [
            'settings' => [
                'refresh_interval' => '5s',
                'number_of_shards' => 1,
                'number_of_replicas' => 0,
            ],
            'mappings' => [
                '_default_' => [
                    '_all' => [
                        'enabled' => false,
                    ],
                ],
            ],
        ];
    }


}
